package com.horay.springbootdemo.transactional.service.impl;

import com.horay.springbootdemo.common.model.Foo;
import com.horay.springbootdemo.transactional.service.FooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2019/11/6 22:43
 */
@Service
public class FooServiceImpl implements FooService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert() {
        jdbcTemplate.update(Foo.INSERT_SQL, "AAAAAA");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertThenRollback() {
        jdbcTemplate.update(Foo.INSERT_SQL, "BBBBBB");
        throw new RuntimeException();
    }

    /**
     * 记录不回滚
     *
     * @param
     * @author heyingcheng
     * @date 2019/11/6 22:57
     * @return void
     */
    @Override
    public void invokeInsertThenRollback() throws RuntimeException {
        insertThenRollback();
    }

}
